(function () {
    var interval = null;
    $("#sub").on("click", function () {
        var url = $("#url").val();
        $.ajax({
            url: "/api/crawl/",
            type: "post",
            data: {'url': url},
            dataType: "json",
            success: function (start) {
                console.log(start);
                if (start.status == "started") {
                    interval = setInterval(function () {
                        askStatus(start, interval);
                    }, 1000);
                }
            },
            error: function (start) {
                console.log('Start Error', start);
            }
        })
        return false;
    })
})();

function askStatus(start, interval) {
    $.ajax({
        url: "/api/crawl/",
        type: "get",
        async: false,
        data: {'task_id': start.task_id, 'unique_id': start.unique_id},
        dataType: "json",
        success: function (end) {
            console.log('end', end);
            if (end.status == "finished") {
                if (interval != null) {
                    clearTimeout(interval);
                } else {
                    console.log("定时器无法正常关闭！！！");
                }
            } else {
                console.log("continue...");
            }
        },
        error: function (end) {
            console.log('End Error', end);
        }
    });
}